/**
 * BoundedBuffer.java - 2013-8-15
 *
 * Licensed Property to China UnionPay Co., Ltd.
 * 
 * (C) Copyright of China UnionPay Co., Ltd. 2010
 *     All Rights Reserved.
 *
 * Project Info: China UnionPay Internet Acquiring Project
 * 
 * Modification History:
 * =============================================================================
 *   Author         Date          Description
 *   ------------ ---------- ---------------------------------------------------
 *        
 * =============================================================================
 */
package com.lijin.workshop.test.thread;

/**
 * Description: 
 *
 * (C) Copyright of China UnionPay Co., Ltd. 2010.
 *
 */
public class BoundedBuffer {

	private String[] buf;
	private int in = -1, out = -1;
	private int count = 0;
	
	public BoundedBuffer(int size) {
		buf = new String[size];
	}
	
	public synchronized void put(String s) {
		while(count >= buf.length) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		++ count;
		buf[++in % buf.length] = s;
		System.out.println(s);
		
		notify();
	}
	
	public String get() {
		while(count == 0);
		
		synchronized (this) {
			-- count;
			String s = buf[++out % buf.length];
			return s;
		}
	}
}
